package org.sidora.strata.explorer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.logging.Level;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.sidora.core.Sector;
import org.sidora.core.Site;
import org.sidora.core.Zone;
import org.sidora.core.util.LoggerProvider;
import org.sidora.core.util.ParameterCollector;
import org.sidora.strata.SidoraTools;
import org.sidora.strata.Storage;


/**
 * Sector explorer form
 * @author Enric Tartera, Juan Manuel Gimeno, Roger Masgoret
 * @version 1.0
 */
final class SectorExplorerTopComponent extends TopComponent {
    
    private static SectorExplorerTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    
    private static final String PREFERRED_ID = "SectorExplorerTopComponent";
    
    private SectorExplorerTopComponent() {
        System.setProperty(
            "Quaqua.tabLayoutPolicy","wrap"
         );
         // set the Quaqua Look and Feel in the UIManager
         try { 
              UIManager.setLookAndFeel(
                  "ch.randelshofer.quaqua.QuaquaLookAndFeel"
              );
         // set UI manager properties here that affect Quaqua
         
         } catch (Exception e) {
             // take an appropriate action here
             
         }
         // insert your application initialization code here
        initComponents();
        setName(NbBundle.getMessage(SectorExplorerTopComponent.class, "CTL_SectorExplorerTopComponent"));
        setToolTipText(NbBundle.getMessage(SectorExplorerTopComponent.class, "HINT_SectorExplorerTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonDelSector = new javax.swing.JButton();
        jButtonDelSector.putClientProperty(
            "Quaqua.Button.style","square"
        );
        jButtonAddSector = new javax.swing.JButton();
        jButtonAddSector.putClientProperty(
            "Quaqua.Button.style","square"
        );
        jScrollPane1 = new javax.swing.JScrollPane();
        jListSector = new javax.swing.JList();
        jListSector.putClientProperty(
            "Quaqua.List.style", "striped"
        );

        org.openide.awt.Mnemonics.setLocalizedText(jButtonDelSector, "-");
        jButtonDelSector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDelSectorActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButtonAddSector, "+");
        jButtonAddSector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddSectorActionPerformed(evt);
            }
        });

        jListSector.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListSectorValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListSector);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(40, Short.MAX_VALUE)
                .add(jButtonAddSector)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButtonDelSector))
        );

        layout.linkSize(new java.awt.Component[] {jButtonAddSector, jButtonDelSector}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonDelSector)
                    .add(jButtonAddSector)))
        );

        layout.linkSize(new java.awt.Component[] {jButtonAddSector, jButtonDelSector}, org.jdesktop.layout.GroupLayout.VERTICAL);

    }// </editor-fold>//GEN-END:initComponents

    private void jListSectorValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListSectorValueChanged
        
         SidoraTools.showElement();
         Storage.setSector((Sector) jListSector.getSelectedValue());
         Storage.setFeature(null);
         Storage.getFeatureExplorerAction().fillFeatureItems();
         Storage.getSectorAction().actionPerformed();
         /*
        if (jListSector.getValueIsAdjusting()){
            //long lng = System.nanoTime();
            //System.out.println("Inici: "+(lng - System.nanoTime()));
            SidoraTools.showElement();
            //System.out.println("Amagar finestres: "+(lng - System.nanoTime()));
            
//            Storage.getSectorAction().saveFields();     
//            System.out.println("Salvar Sector: "+(lng - System.nanoTime()));
            Storage.setSector((Sector) jListSector.getSelectedValue());
            //System.out.println("Guardar Sector a Storage: "+(lng - System.nanoTime()));
//            Storage.getFeatureAction().saveFields();
//            System.out.println("Gurader Feature: "+(lng - System.nanoTime()));
            Storage.setFeature(null);
            //System.out.println("Fica feature a null: "+(lng - System.nanoTime()));
//            Storage.setContext(null);
//            System.out.println("Fica context a null: "+(lng - System.nanoTime()));            
            Storage.getFeatureExplorerAction().fillFeatureItems();
            //System.out.println("Fill Feature: "+(lng - System.nanoTime()));
//            Storage.getContextFormAction().saveFields();
//            System.out.println("Guardar Context: "+(lng - System.nanoTime()));
//            Storage.setContext(null);
//            System.out.println("Ficar context a null: "+(lng - System.nanoTime()));
//            Storage.getContextExplorerAction().fillContextItems();
//            System.out.println("Omplir contexts list: "+(lng - System.nanoTime()));
            Storage.getSectorAction().actionPerformed();
            //System.out.println("Mostrar finestres del sector: "+(lng - System.nanoTime()));
        }
          */
         
    }//GEN-LAST:event_jListSectorValueChanged

    private void jButtonAddSectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddSectorActionPerformed
        addSector();
        fillSectorItems();
    }//GEN-LAST:event_jButtonAddSectorActionPerformed

    private void jButtonDelSectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDelSectorActionPerformed

    }//GEN-LAST:event_jButtonDelSectorActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddSector;
    private javax.swing.JButton jButtonDelSector;
    private javax.swing.JList jListSector;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized SectorExplorerTopComponent getDefault() {
        if (instance == null) {
            instance = new SectorExplorerTopComponent();
        }
        return instance;
    }
    
    /**
     * Obtain the SectorExplorerTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized SectorExplorerTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot find SectorExplorer component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof SectorExplorerTopComponent) {
            return (SectorExplorerTopComponent)win;
        }
        ErrorManager.getDefault().log(ErrorManager.WARNING, "There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }
    
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }
    
    public void componentOpened() {
        this.setDisplayName("Sector");
    }
    
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    /** replaces this in object stream */
    public Object writeReplace() {
        return new ResolvableHelper();
    }
    
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    final static class ResolvableHelper implements Serializable {
        private static final long serialVersionUID = 1L;
        public Object readResolve() {
            return SectorExplorerTopComponent.getDefault();
        }
    }
    
    
    private static void addSector(){
        Zone zone = Storage.getZone();

        if (zone != null){
            ParameterCollector param = new ParameterCollector();
            Sector.Inventory si = new Sector.Inventory();
            
            param.put("zone",zone);
            
            Sector sector = null;
            try {
                sector = si.getNewObject(param);
                sector.setClosed(false);
                
                si.setObject(sector);
            }catch (Exception e1) {
                LoggerProvider.getInstance().log(Level.WARNING,"Exception",e1);
            }
        }else{
            LoggerProvider.getInstance().fine("No hay un Site seleccionado.");
        }
    }
    
    public void fillSectorItems(){
        Zone zone = Storage.getZone();
        if (zone != null){
            DefaultListModel sectorModel = new DefaultListModel();
            
            try {
                List list = Sector.Inventory.getList(zone);//Site.Inventory.getList();
                TreeSet <Sector> sectors = new TreeSet<Sector>();
                
                Iterator iter2 = list.iterator();
                while(iter2.hasNext()){
                    Sector sector = (Sector) iter2.next();
                    sectors.add(sector);
                }
                
                //Ho fiquem en un TreeSet per a tenir la col�lecci� ordenada
                Iterator iter = sectors.iterator();
                while (iter.hasNext()){
                    sectorModel.addElement(iter.next());
                }
                
            } catch (Exception e1) {
                LoggerProvider.getInstance().log(Level.WARNING,"Exception",e1);
            }
            jListSector.setModel(sectorModel);
        } else {
            jListSector.setModel(new DefaultListModel());
        }
    }
    
}
